package com.wawayaya.db.bookshelf;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.wawayaya.api.ApiResponseData;
import com.wawayaya.api.remote.ApiConstans;
import com.wawayaya.bean.Area;
import com.wawayaya.bean.Banner;
import com.wawayaya.bean.BookShelf;
import com.wawayaya.bean.Books;
import com.wawayaya.bean.Config;
import com.wawayaya.bean.Notice;
import com.wawayaya.bean.Support;
import com.wawayaya.bean.Timestamp;
import com.wawayaya.bean.User;
import com.wawayaya.db.entitydao.AreaEntityDao;
import com.wawayaya.db.entitydao.BannerEntityDao;
import com.wawayaya.db.entitydao.BookEntityDao;
import com.wawayaya.db.entitydao.BookShelfEntityDao;
import com.wawayaya.db.entitydao.ConfigEntityDao;
import com.wawayaya.db.entitydao.NoticeEntityDao;
import com.wawayaya.db.entitydao.SupportEntityDao;
import com.wawayaya.db.entitydao.UserEntityDao;


/**
 * Created by david on 15/8/24.
 */
public class ShelfSaveDBThread extends Thread{

    ApiResponseData data;

    public  ShelfSaveDBThread(){

    }
    public ShelfSaveDBThread(ApiResponseData data){
        this.data = data;
    }
    @Override
    public void run() {
        super.run();
        updateData(data);
    }

    /**
     * update 业务
     */
    public void   updateData(final ApiResponseData data){
        //Log.e(TAG,"updateData......update array."+data.getUpdateAry().toString()+"data type:"+data.getRquestType());
        switch (data.getRquestType()){
            case ApiConstans.USER_LOGIN:
            case ApiConstans.USER_REGISTER:
            case ApiConstans.LOGINJOYREADER:
                userOperate(data);
                break;
            case ApiConstans.DEVICE:
                userDeviceOperate(data);
                break;
            case ApiConstans.CONFIG:
                configOperate(data);
                break;
            case ApiConstans.BANNER:
                bannerOperate(data);
                break;
            case ApiConstans.BOOKSHELF:
                bookShelfOperate(data);
                break;
            case ApiConstans.AREA:
                areaOperate(data);
                break;
            case ApiConstans.BOOK:
                bookByShelfOperate(data);
                break;
            case ApiConstans.NOTICE:
                noticeOperate(data);
                break;

            case ApiConstans.SUPPORT:
                supportOperate(data);
                break;
        }
    }


    private synchronized void supportOperate(final ApiResponseData  data){
        SupportEntityDao supportFileDao = new SupportEntityDao();
        Support support =  new Support();
        try{
            if(null!=data.getUpdateAry()){

                for(int i=0;i<data.getUpdateAry().length();i++){
                    support = ((Support)support.clone()).jsonToVO((JSONObject)data.getUpdateAry().get(i));
                    supportFileDao.replaceEntity(support);
                    // Log.e(TAG,"返回supportfile...."+support.toString());
                }
            }
            if(null!=data.getDeleteAry()){
                for(int i=0;i<data.getDeleteAry().length();i++){
                    JSONObject  jo=  data.getDeleteAry().optJSONObject(i);
                    supportFileDao.deleteEntityByKey(jo.optString("sfKey"));
                }
            }
          /*  Timestamp timestamp = new Timestamp();
            timestamp.setuId(data.getUid());
            timestamp.setSupportFile(data.getServerTimestamp());*/

          /*  TimestampEntityDao timestampDao = new TimestampEntityDao();
            timestampDao.updateEntityBywhere(timestamp);*/
        }catch (JSONException e){
            e.printStackTrace();
        }
    }


    private synchronized void  noticeOperate(final ApiResponseData  data){
        NoticeEntityDao noticeDao = new NoticeEntityDao();
        try{
            if(null!=data.getUpdateAry()){
                Notice notice =  new Notice();
                for(int i=0;i<data.getUpdateAry().length();i++){
                    notice =  ((Notice)notice.clone()).jsonToVO((JSONObject)data.getUpdateAry().get(i));
                    noticeDao.replaceEntity(notice);
                    //  Log.e(TAG,"返回noticeOperate...."+notice.toString());
                }
            }
            if(null!=data.getDeleteAry()){
                for(int i=0;i<data.getDeleteAry().length();i++){
                    JSONObject  jo =data.getDeleteAry().optJSONObject(i);
                    noticeDao.deleteEntityByKey(jo.optLong("noticeId"));
                }
            }
       /*     Timestamp  timestamp = new Timestamp();
            timestamp.setuId(data.getUid());
            timestamp.setNotice(data.getServerTimestamp());

            TimestampEntityDao timestampDao = new TimestampEntityDao();
            timestampDao.updateEntityBywhere(timestamp);*/
        }catch (JSONException e){
            e.printStackTrace();
        }

    }


    private synchronized  void  bookByShelfOperate(final ApiResponseData  data){
        BookEntityDao booksDao =new BookEntityDao();
        try{
            if(null!=data.getUpdateAry()){
                Books book =  new Books();
                for(int i=0;i<data.getUpdateAry().length();i++){
                    book = ((Books)book.clone()).jsonToVO((JSONObject)data.getUpdateAry().get(i));
                    booksDao.replaceEntity(book);
                    // Log.e(TAG,"返回 book ...."+book.toString());
                }
            }
            if(null!=data.getDeleteAry()){
                for(int i=0;i<data.getDeleteAry().length();i++){
                    JSONObject  jo=  data.getDeleteAry().optJSONObject(i);
                    booksDao.deleteEntityByKey(jo.optInt("bookId"));
                }
            }
            //update timestamp
            /*
            Timestamp  timestamp = new Timestamp();
            timestamp.setuId(data.getUid());
            timestamp.setBooks(data.getServerTimestamp());

            TimestampEntityDao timestampDao = new TimestampEntityDao();
            timestampDao.updateEntityBywhere(timestamp);*/
        }catch (JSONException e){
            e.printStackTrace();
        }
    }


    /**
     * 未修改
     * @param data
     */
    private synchronized  void  bookShelfOperate(final ApiResponseData  data){
        BookShelfEntityDao shelfDao = new BookShelfEntityDao();
        try{

            if(null!=data.getUpdateAry()){
                BookShelf shelf =  new BookShelf();
                for(int i=0;i<data.getUpdateAry().length();i++){
                    shelf = ((BookShelf)shelf.clone()).jsonToVO((JSONObject)data.getUpdateAry().get(i));
                    shelfDao.replaceEntity(shelf);
                    //Log.e(TAG,"返回 book ...."+shelf.toString());
                }
            }
            if(null!=data.getDeleteAry()){
                for(int i=0;i<data.getDeleteAry().length();i++){
                    JSONObject  jo=  data.getDeleteAry().optJSONObject(i);
                    shelfDao.deleteEntityByKey(jo.optLong("bsId"));
                }
            }
      /*      Timestamp  timestamp = new Timestamp();
            timestamp.setuId(data.getUid());
            timestamp.setBookSelf(data.getServerTimestamp());

            TimestampEntityDao timestampDao = new TimestampEntityDao();
            timestampDao.updateEntityBywhere(timestamp);*/
        }catch (JSONException e){
            e.printStackTrace();
        }

    }


    private synchronized void  configOperate(final ApiResponseData  data){
        ConfigEntityDao configDao = new ConfigEntityDao();
        Config config =  new Config();
        try{
            if(null!=data.getUpdateAry()){

                for(int i=0;i<data.getUpdateAry().length();i++){
                    config =  ((Config)config.clone()).jsonToVO((JSONObject)data.getUpdateAry().get(i));
                    configDao.replaceEntity(config);
                    //  Log.e(TAG,"返回 configOperate ...."+config.toString());
                }
            }
            if(null!=data.getDeleteAry()){
                for(int i=0;i<data.getDeleteAry().length();i++){
                    config =  ((Config)config.clone()).jsonToVO((JSONObject)data.getDeleteAry().get(i));
                    configDao.deleteEntityByKey(config.getValue());
                }
            }

          /*  Timestamp  timestamp = new Timestamp();
            timestamp.setuId(data.getUid());
            timestamp.setConfig(data.getServerTimestamp());

            TimestampEntityDao timestampDao = new TimestampEntityDao();
            timestampDao.updateEntityBywhere(timestamp);*/
        }catch (JSONException e){
            e.printStackTrace();
        }

    }

    private synchronized void  bannerOperate(final ApiResponseData  data){
        BannerEntityDao bannerDao = new BannerEntityDao();
        Banner banner =  new Banner();
        try{
            if(null!= data.getUpdateAry()){

                for(int i=0;i< data.getUpdateAry().length();i++){
                    banner =  ((Banner)banner.clone()).jsonToVO((JSONObject) data.getUpdateAry().get(i));
                    Log.e("bannerOperate", "返回 bannerOperate........." + banner.toString());
                    bannerDao.insertOrReplaceByWhere(banner);
                }
            }
            if(null!=data.getDeleteAry()){
                //Log.e("DBManager","array...delete ......"+data.getDeleteAry());
                for(int i=0;i< data.getDeleteAry().length();i++){
                    banner =  ((Banner)banner.clone()).jsonToVO((JSONObject) data.getDeleteAry().get(i));
                    bannerDao.deleteEntityByKey(banner.getBannerId());
                }
            }
     /*       Timestamp  timestamp = new Timestamp();
            timestamp.setuId(data.getUid());
            timestamp.setBanner(data.getServerTimestamp());

            TimestampEntityDao timestampDao = new TimestampEntityDao();
            timestampDao.updateEntityBywhere(timestamp);*/
        }catch (JSONException e){
            e.printStackTrace();
        }

    }

    private synchronized void  areaOperate(final ApiResponseData  data){
        AreaEntityDao areaEntityDao = new AreaEntityDao();
        Area area = new Area();
        try{
            if(null!=data.getUpdateAry()) {
                for (int i = 0; i < data.getUpdateAry().length(); i++) {
                    area = ((Area) area.clone()).jsonToVO((JSONObject) data.getUpdateAry().get(i));
                    areaEntityDao.replaceEntity(area);
                }
            }
            if(null!=data.getDeleteAry()){
                for(int i=0;i< data.getDeleteAry().length();i++){
                    area =  ((Area)area.clone()).jsonToVO((JSONObject)data.getDeleteAry().get(i));
                    areaEntityDao.deleteEntityByKey(area.getAreaId());
                    //Log.e(TAG, "返回 configOperate ...." + area.toString());
                }
            }
        /*    Timestamp  timestamp = new Timestamp();
            timestamp.setuId(data.getUid());
            timestamp.setArea(data.getServerTimestamp());

            TimestampEntityDao timestampDao = new TimestampEntityDao();
            timestampDao.updateEntityBywhere(timestamp);*/
        }catch (JSONException e){
            e.printStackTrace();
        }

    }

    private synchronized void  userOperate(final ApiResponseData  data){
        UserEntityDao userEntityDao = new UserEntityDao();
        User user = new User().jsonToVO(data.getRetinfo());
        Log.e("userinfor", "userid:" + user.getUserId());
        try{
            if(null==data.getDeleteAry()){
                // Log.e(TAG,"返回 userOperate ...."+user.toString());
                userEntityDao.replaceEntity(user);
            }

        /*    Timestamp  timestamp = new Timestamp();
            timestamp.setuId(data.getUid());
            timestamp.setUsers(data.getServerTimestamp());

            TimestampEntityDao timestampDao = new TimestampEntityDao();
            timestampDao.updateEntityBywhere(timestamp);*/
        }catch(Exception  e){
            e.printStackTrace();
        }
    }

    private synchronized void  userDeviceOperate(final ApiResponseData   data){
        UserEntityDao  userEntityDao = new UserEntityDao();
        User user = new User().jsonToVO(data.getRetinfo());
        //Log.e("userinfor","userid:"+user.getUid()+" device emdial:"+user.getEmail());
        try{
            //  Log.e(TAG,"返回 userOperate ...."+user.toString());
            userEntityDao.replaceEntity(user);
/*
            Timestamp  timestamp = new Timestamp();
            timestamp.setuId(data.getUid());
            timestamp.setUsers(data.getServerTimestamp());

            TimestampEntityDao timestampDao = new TimestampEntityDao();
            timestampDao.updateEntityBywhere(timestamp);*/
        }catch(Exception  e){
            e.printStackTrace();
        }
    }
}
